import time
import threading
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import xmlrpclib
import Queue
import string

#test 2

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)
    
class OperParams:
    def __init__(self):
        self.UserID = "test"
        self.HostTimeStamp = time.time()
        self.OnPage = 0
        self.Others = ""

class Log:
    def __init__(self):
        self.UserID = "test"
        self.HostTimeStamp = time.time()      
        self.LastSyncHostTimeStamp = time.time()
        self.CurrentSyncHostTimeStamp = time.time()
        self.IsSuccess = False      
        
    
class DataHubService:
    def __init__(self, role, hostname):
        self.SkypeProxy = xmlrpclib.ServerProxy('http://localhost:62750')
        self.GUIProxy = xmlrpclib.ServerProxy('http://localhost:62751')
        self.UserList= self.SkypeProxy.getuserlist()
        self.OpQueue = Queue.Queue()
        self.SyncInfo = {}
        for user in self.UserList:
            self.SyncInfo[user] = time.time()        
        self.SyncLog = []  
        # two possible roles, host or client
        self.role = role   
        self.hostuser = hostname             
        
    def decode(self, text):
        params = string.split(text, sep = '_')
        if params[0] == "WB":
            self.SkypeProxy.unpack(text)
        print "decode is working"
        return "abcdedf"
    
    def encode(text):
        result = ''
        params = string.split(text)
        if params[0] == "WB":
            result = str(time.ctime()) + text
        elif params[0] == "UL":
            result = str(time.ctime()) + text
        print "encode is working"
    
    def receiving(self, message):
        params = string.split(message)
        
        print params
        if params[0] == "WB":
            tempOper = OperParams()
            tempOper.HostTimeStamp = time.time()
            tempOper.UserID = params[1]# I am not sure whether the index is right
            tempOper.OpPage = 1
            self.OpQueue.put(tempOper)
            print tempOper
        print self.OpQueue
        return True
    
    def synchronize(self):
        tempOper = OperParams()
        print self.UserList
        print self.OpQueue.qsize()
        if self.OpQueue.empty():
            print "OpQueue is empty"
        while self.OpQueue.empty() == False:
            print "Line 74"
            tempOper = self.OpQueue.get()  
            print "line78" + str(self.OpQueue.qsize())                       
            allDelivered = True                           
            for user in self.UserList:
                print "track is beginning:"
                print "user: " + user
                print "SyncInfo: " + str(self.SyncInfo)
                print "tempOper's hosttimestamp" + str(tempOper.HostTimeStamp)
                if tempOper.HostTimeStamp > self.SyncInfo[user]:
                    #update SyncInfo
                    self.SyncInfo[user] = tempOper.HostTimeStamp
                    
                    #append SyncLog
                    newLog = Log()
                    newLog.UserID = user
                    newLog.HostTimeStamp = tempOper.HostTimeStamp
                    message = 'test message'
                    print user
                    if self.SkypeProxy.sendmessage(message,user) == True:  
                        newLog.IsSuccess = True
                    else:
                        newLog.IsSuccess = False
                        allDelivered = False
                    self.SyncLog.append(newLog)                   
            if allDelivered == False:
                self.OpQueue.put(tempOper)
        for log in self.SyncLog:
            print log.UserID
            print log.HostTimeStamp
        return True
    
    def callhost(self, message):
        self.SkypeProxy.sendmessage(message, self.hostuser)
        return True
        

print "It is here... Line 62"   

myService = SimpleXMLRPCServer(("localhost", 62752))          
myService.register_introspection_functions()     
dataHubService_object = DataHubService()               
myService.register_instance(dataHubService_object)
#myService.register_function(DataHubService.decode)

print "dataHubService is running:"
print "Listening on port 62752"
myService.serve_forever()